/**
 * 居民注册信息
 * by zlh
 */
//功能栏
ResidentBtn = Ext.extend(Ext.Toolbar,{
	constructor : function(){
		ResidentBtn.superclass.constructor.call(this,{
			items : [{
				text : '查询',
				iconCls : 'icon-search',
				handler : this.onOpenSearchWin,
				scope : this
			}]
		})
	},
	onOpenSearchWin : function(){
		this.fireEvent('onOpenSearchWin',this);
	}
});
SearchPanel = Ext.extend(Ext.form.FormPanel,{
	constructor : function(b){
		Ext.applyIf(this,b);
		SearchPanel.superclass.constructor.call(this,{
			frame : true,
			border : false,
			autoScroll : true,
			labelAlign : 'right',
			items : [{
				layout : "column",
				border : false,
				items : [{
	    			columnWidth : .05,
					layout : "form",
					defaultType : 'textfield',
					border : false,
					labelWidth : 1,
		    		items : [{
		    			xtype : 'radio',
		    			value : 1,
		    			name : 'checkType',
		    			checked : true
		    		}]
				},{
					columnWidth : .4,
					layout : "form",
					border : false,
					labelWidth : 60,
					items : [{
		    			fieldLabel : '证件类型',
						hiddenName : "zjlx",
						xtype : 'combo',
						forceSelection : true, //用户不能自己输入,只能选择列表中有的记录 
						mode : "remote",
						triggerAction : "all",
						id : 'search_zjlx',
						store : this.getZgjgStore('HR01_01_002_01'),
						valueField : "key",
						displayField : "value",
						anchor : '90%'
		    		}]
				},{
					columnWidth : .55,
					layout : "form",
					defaultType : 'textfield',
					labelWidth : 40,
					border : false,
					items : [{
		    			fieldLabel : '号码',
		    			id : 'search_zjNum',
						name : "zjNum"
		    		}]
				}]
			},{
				layout : "column",
				border : false,
				items : [{
	    			columnWidth : .05,
					layout : "form",
					defaultType : 'textfield',
					border : false,
					labelWidth : 1,
		    		items : [{
		    			xtype : 'radio',
		    			name : 'checkType',
		    			value : 2,
		    			listeners : {
		    				check : function(radio){
		    					this.setSearchDisabled(radio.checked);
		    				},
		    				scope : this
		    			}
		    		}]
				},{
	    			columnWidth : .2,
					layout : "form",
					defaultType : 'textfield',
					border : false,
					labelWidth : 40,
		    		items : [{
		    			fieldLabel : '姓名',
		    			disabled : true,
		    			id : 'search_xm',
						name : "xm",
						anchor : '100%'
		    		}]
				},{
	    			columnWidth : .3,
					layout : "form",
					defaultType : 'datetime',
					border : false,
					labelWidth : 70,
		    		items : [{
		    			fieldLabel : '出生日期从',
						xtype : 'datefield',
						format : 'Y-m-d',
						disabled : true,
						id : 'search_startCsrq',
					 	name : 'startCsrq',
						anchor : '100%'
		    		}]
				},{
	    			columnWidth : .2,
					layout : "form",
					border : false,
					labelWidth : 10,
		    		items : [{
		    			fieldLabel : '到',
		    			labelSeparator : '',
						xtype : 'datefield',
						disabled : true,
						id : 'search_endCsrq',
						format : 'Y-m-d',
					 	name : 'endCsrq',
						anchor : '100%'
		    		}]
				},{
	    			columnWidth : .25,
					layout : "form",
					border : false,
					labelWidth : 40,
		    		items : [{
		    			fieldLabel : '性别',
						hiddenName : "xb",
						xtype : 'combo',
						forceSelection : true, //用户不能自己输入,只能选择列表中有的记录 
						mode : "remote",
						triggerAction : "all",
						disabled : true,
						id : 'search_xb',
						store : this.getZgjgStore('HR02_02_001'),
						valueField : "key",
						displayField : "value",
						anchor : '100%'
		    		}]
				}]
			}]
		})
	},
	setSearchDisabled : function(flag){
		if(flag){
			Ext.getCmp('search_xm').setDisabled(false);
			Ext.getCmp('search_startCsrq').setDisabled(false);
			Ext.getCmp('search_endCsrq').setDisabled(false);
			Ext.getCmp('search_xb').setDisabled(false);
			Ext.getCmp('search_zjlx').setDisabled(true);
			Ext.getCmp('search_zjNum').setDisabled(true);
		}else{
			Ext.getCmp('search_xm').setDisabled(true);
			Ext.getCmp('search_startCsrq').setDisabled(true);
			Ext.getCmp('search_endCsrq').setDisabled(true);
			Ext.getCmp('search_xb').setDisabled(true);
			Ext.getCmp('search_zjlx').setDisabled(false);
			Ext.getCmp('search_zjNum').setDisabled(false);
		}
	},
	getZgjgStore : function(type){
		var zgjgStore = new Ext.data.JsonStore({
			url : '../../hrcode.do',
			baseParams : {
				type : type
			},
			method : 'POST',
			fields : ['value','key']
		});
		return zgjgStore;
	}
});
SearchWin = Ext.extend(Ext.Window,{
	searchPanel : null,
	constructor : function(b){
		this.searchPanel = new SearchPanel(b);
		SearchWin.superclass.constructor.call(this,{
			modal : true,
			layout : 'fit',
			buttonAlign : "center", //按钮居中
			height : 200,
			width : 600,
			constrain : true,
			items : this.searchPanel,
			buttons : [{
				text : '确定',
				iconCls : "btn-ok",
				handler : this.onSearch,
				scope : this
			},'-',{
				text : '取消',
				iconCls : "btn-cancel",
				handler : function(){
					this.close();
				},
				scope : this
			}]
		})
	},
	onSearch : function(){
		this.fireEvent('onSearch',this.searchPanel,this);
	}
});
ResidentGridPanel = Ext.extend(Ext.grid.GridPanel,{
	residentBtn : null,
	residentPanel : null,
	residentStore : null,
	residentCSM : null,
	residentCM : null,
	constructor : function(){
		this.residentBtn = new ResidentBtn();
		this.initUIComponents();
		ResidentGridPanel.superclass.constructor.call(this,{
			title : '▪ 您现在所在的位置：居民注册信息',
			border : true,
			renderTo : Ext.getBody(),
			viewConfig : {
				forceFit : true
			},
			stripeRows : true,//斑马线
			tbar : this.residentBtn,
			colModel : this.residentCM,
			store : this.residentStore,
			sm : this.residentCSM,
			bbar : new Ext.PagingToolbar({
				pageSize : 20,
				store : this.residentStore,
				displayInfo : true,
				displayMsg : "显示第{0}-{1}条记录，一共{2}条记录",
				emptyMsg : "没有记录"
			})
		}),
		this.residentBtn.on('onOpenSearchWin',this.onOpenSearchWin,this);
		this.loadList();
	},
	initUIComponents : function(){
		this.residentCSM = new Ext.grid.CheckboxSelectionModel();
		this.residentStore = new Ext.data.JsonStore({
			url : '../../resident/list.do',
			method : 'POST',
			totalProperty : 'totalProperty',
			root : 'data',
			fields : ["grid","xm","xbCn","csrq","dz","sfzh","smkh"]
		});
		this.residentCM = new Ext.grid.ColumnModel([this.residentCSM,{
			header : '居民注册号',
			dataIndex : 'grid',
			align : 'center'
		},{
			header : '姓名',
			dataIndex : 'xm',
			align : 'center'
		},{
			header : '性别',
			dataIndex : 'xbCn',
			align : 'center'
		},{
			header : '出生日期',
			dataIndex : 'csrq',
			align : 'center'
		},{
			header : '个人地址',
			width : 150,
			dataIndex : 'dz',
			align : 'center'
		},{
			header : '身份证号',
			dataIndex : 'sfzh',
			align : 'center'
		},{
			header : '市民卡',
			dataIndex : 'smkh',
			align : 'center'
		}]);
	},
	loadList : function(_searchPanel,_win){
		if(_searchPanel != null){
			var values = _searchPanel.getForm().getValues();
			this.getStore().baseParams={
				zjlx : values.zjlx,
				zjNum : values.zjNum,
				xm : values.xm,
				startCsrq : values.startCsrq,
				endCsrq : values.endCsrq,
				xb : values.xb
			}
		}
		this.getStore().load({
			params : {
				start : 0,
				limit : 20
			},
			callback : function(r,options,success){
				if(success){
					if(_win != null){
						_win.close();
					}
				}else{
					Ext.ux.Toast.msg('信息提示','服务器连接断开，请联系管理员!');
				}
			}
		});
	},
	insertUpdateRecord : function(_win,_form){
		var self = this;
		_form.form.submit({
			waitTitle : "请稍等",
			waitMsg : "正在保存您的信息",
			success : function(form,action){
				if(action.result.success){
					Ext.ux.Toast.msg('提示信息',"保存成功！");
					_win.close();
					self.getStore().reload();
				}
			},
			failure : function(form, action) {
				if(action.result == null){
					Ext.ux.Toast.msg('提示信息',"保存信息失败,请联系管理员！");
				}else{
					Ext.ux.Toast.msg('提示信息',action.result.message);
				}
			}
		});
	},
	onOpenSearchWin : function(){
		var searchWin = new SearchWin();
		searchWin.mon(searchWin,'onSearch',this.loadList,this);
		searchWin.show();
	}
});
